/*
 * auth.c
 *
 *  Created on: 2019年1月20日
 *      Author: 0yfj0
 */

#include <string.h>
#include "crc32.h"
#include "sys.h"
#include "sys_auth.h"

uint32_t auth_random(void)
{
	uint32_t rand = AUTH_BASE;

	switch (sys.t100ms & 0x03)
	{
	case 0:
	case 1:
		rand += sys.sec + (sys.t100ms & 0x03FF);
		break;
	case 2:
	case 3:
		rand += sys.dsec + (sys.t100ms & 0x03FF);
		break;
	}
	return rand;
}

uint32_t auth_pubkey(uint32_t rand, uint32_t key)
{
	uint32_t data[4];

	data[0] = AUTH_BASE;
	data[1] = key;
	data[2] = rand;
	data[3] = key + rand;
	return crc32_compute((uint8_t*) data, sizeof(data), NULL);
}
